//        https://www.nowcoder.com/practice/7a71a88cdf294ce6bdf54c899be967a2





class Solution
{
private:
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    vector<vector<int>> memo;
    int n, m;
public:
    int dfs(vector<vector<int> >& matrix, int i, int j)
    {
        if (memo[i][j] != 0)
            return memo[i][j];
        int ans = 1;
        int num = matrix[i][j];
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && y >= 0 && x < m && y < n && num < matrix[x][y])
                ans = max(ans, 1 + dfs(matrix, x, y));
        }
        memo[i][j] = ans;
        return ans;
    }
    int solve(vector<vector<int> >& matrix)
    {
        m = matrix.size();
        n = matrix[0].size();
        memo.resize(m, vector<int>(n));
        int ans = 1;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                ans = max(ans, dfs(matrix, i, j));
        return ans;
    }
};